package br.project.cdd.presentation;

import br.project.cdd.vo.CddVo;
import br.project.cdd.bean.CddBean;
import java.util.ArrayList;
import javax.swing.table.DefaultTableModel;
import javax.swing.JPanel;
import java.awt.Rectangle;
import java.awt.Dimension;

public class CadastrarCddForm extends JPanel{

    public CadastrarCddForm() {
        initComponents();
    }

    
    private void initComponents() {
    	
        lblNome = new javax.swing.JLabel();
        txtNome = new javax.swing.JTextField();
        lblLocalidade = new javax.swing.JLabel();
        txtLocalidade = new javax.swing.JTextField();
        lblUf = new javax.swing.JLabel();
        txtUf = new javax.swing.JTextField();
        btnConsultar = new javax.swing.JButton();
        btnInserir = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblConteudo = new javax.swing.JTable();

        this.setLayout(null);
        setMinimumSize(new java.awt.Dimension(433, 323));
        this.setSize(new Dimension(484, 513));
        
        lblNome.setText("Nome do CDD:");
        lblNome.setBounds(55, 10, 80, 16);
        this.add(lblNome, null);
        txtNome.setBounds(162, 9, 170, 20);

        lblLocalidade.setText("Localidade:");
        this.add(txtNome, null);
        lblLocalidade.setBounds(54, 44, 65, 16);
        this.add(lblLocalidade, null);
        txtLocalidade.setBounds(162, 43, 170, 20);

        lblUf.setText("UF:");
        this.add(txtLocalidade, null);
        lblUf.setBounds(54, 84, 17, 16);
        this.add(lblUf, null);
        txtUf.setBounds(164, 82, 167, 20);

        btnConsultar.setText("Consultar");
        this.add(txtUf, null);
        btnConsultar.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btnConsultarMouseClicked(evt);
            }
        });
        btnConsultar.setBounds(362, 33, 89, 26);

        btnInserir.setText("Inserir");
        this.add(btnConsultar, null);
        btnInserir.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btnInserirMouseClicked(evt);
            }
        });
        btnInserir.setBounds(363, 79, 89, 26);

        tblConteudo.setModel(new DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Nome do CDD", "Localidade", "UF"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tblConteudo.setBounds(new Rectangle(94, 220, 273, 223));
        tblConteudo.setVisible(false);
        this.add(btnInserir, null);
        this.add(jScrollPane1, null);
        this.add(tblConteudo, null);
        jScrollPane1.setBounds(60, 197, 360, 258);

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-449)/2, (screenSize.height-363)/2, 449, 363);
    }

    private void btnInserirMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnInserirMouseClicked
    	CddVo cdd = new CddVo();
    	cdd.inserirCdd(txtNome.getText(), txtLocalidade.getText(), txtUf.getText());
    	preencherTabela(cdd.selecionarTodosCdds());
    }

    private void btnConsultarMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnConsultarMouseClicked
    	CddVo cdd = new CddVo();
    	preencherTabela(cdd.selecionarTodosCdds());
    }
    
    private void preencherTabela(ArrayList<CddBean> listCdds)
    {
        DefaultTableModel modelo = new DefaultTableModel(null, new String[] { "Nome do CDD", "Localidade", "UF" });
		modelo.setColumnCount(3);
	
		for (CddBean cddBean : listCdds)
		{
	            modelo.addRow(new Object[] { cddBean.getNome_cdd(), cddBean.getLocalidade(), cddBean.getUf() });
		}
	
	    this.tblConteudo.setVisible(true);
		this.tblConteudo.setModel(modelo);
		this.tblConteudo.repaint();		
    }

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new CadastrarCddForm().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnConsultar;
    private javax.swing.JButton btnInserir;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lblLocalidade;
    private javax.swing.JLabel lblNome;
    private javax.swing.JLabel lblUf;
    private javax.swing.JTable tblConteudo;
    private javax.swing.JTextField txtLocalidade;
    private javax.swing.JTextField txtNome;
    private javax.swing.JTextField txtUf;
    // End of variables declaration//GEN-END:variables

}  //  @jve:decl-index=0:visual-constraint="10,10"
